package ems.dev.easycode.controller;

import cn.hutool.core.lang.ConsoleTable;
import cn.hutool.core.util.StrUtil;
import ems.dev.easycode.domain.builder.impl.DomainBuilderImpl;
import ems.dev.easycode.service.builder.ServiceBuilder;
import ems.dev.easycode.service.builder.impl.ServiceBuilderImpl;
import ems.dev.easycode.sql.builder.impl.SqlBuilderImpl;
import ems.dev.easycode.util.Constant;
import ems.dev.easycode.util.Db2Util;
import ems.dev.easycode.util.FileUtil;
import ems.dev.easycode.util.PropertiesUtil;


/**
 * @author 狠穿
 * @version 1.0.0
 * @date 2023/6/23
 * @function
 */
public class EasyController {

    public static void main(String[] args) {
        autoCreate("T_HD_DEPART_INPUT_ITEM");
    }

    private static void autoCreate(String tableName ){

        ConsoleTable consoleTable = new ConsoleTable();
        consoleTable.addHeader("创建内容","是否创建成功","详细内容");

        //自动建文件夹
        String location = PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME,Constant.EASYCODE_GERENATE_LOCATION);
        String tactics = PropertiesUtil.getProValueStr(Constant.EASY_CODE_PROPERTIES_FILE_NAME,Constant.EASYCODE_GERENATE_TACTICS);
        boolean create = FileUtil.autoCreateDirIfAbsent(location,tactics);
        consoleTable.addBody("生成策略", "",StrUtil.equals("CLEAR",tactics)?"先清空后创建":"同名覆盖策略");
        consoleTable.addBody("存放目录",create?"√":"×",create?"文件夹不存在自动创建":"文件夹已经存在无需创建");

        //实体类生成
       try{
           DomainBuilderImpl domainBuilder = new DomainBuilderImpl();
           domainBuilder.createDomain(tableName);
           consoleTable.addBody("实体类","√");
       }catch (Exception e){
           consoleTable.addBody("实体类","×",e.toString());
       }
        //sql生成
       try{
           SqlBuilderImpl sqlBuilder = new SqlBuilderImpl();
           sqlBuilder.createXml(tableName);
           consoleTable.addBody("XML文件","√");
       }catch (Exception e){
           consoleTable.addBody("XML文件","×",e.toString());
       }

        //service生成
        try {
            ServiceBuilder serviceBuilder = new ServiceBuilderImpl();
            serviceBuilder.builderSimpleCRUDMethods(Db2Util.getCurSchema(),tableName);
            consoleTable.addBody("service","√");
        } catch (Exception e) {
            consoleTable.addBody("service","×",e.toString());
        }

        consoleTable.print();
    }
}
